home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Online / News / Thor / HD-Install / thor.lha / rexx / BBSRead / SplitDigest.br < prev   
Text File  |  1997-08-29  |  13KB  |  389 lines

  1. /*
  2. ** $VER: SplitDigest.br 1.01 (20.5.97)
  3. ** by Eirik Nicolai Synnes
  4. **
  5. ** See SortMail.guide for documentation
  6. **
  7. */
  8.  
  9. options results
  10. options failat 31
  11.  
  12. parse arg arguments
  13.  
  14. /*
  15. ** Initialize some variables
  16. */
  17.  
  18. version  = subword(sourceline(2), 4, 1)
  19.  
  20. template = 'SYSTEM/A,CONFERENCE/A,MSGNO/A/N,DESTSYS/A,DESTCONF/A,REPLYADDR/K'
  21.  
  22. globals  = 'args. data. head. text. newmsg. BBSREAD.LASTERROR myerr globals'
  23.  
  24. /*
  25. ** Find/open BBSREAD ARexx port
  26. */
  27.  
  28. if ~show('P', 'BBSREAD') then do
  29.     address(command)
  30.     'Run >NIL: `GetEnv THOR/THORPath`bin/LoadBBSRead'
  31.     if exists('SYS:RexxC/WaitForPort') then 'SYS:RexxC/WaitForPort BBSREAD'
  32.     else 'WaitForPort BBSREAD'
  33.     if (rc = 5) then do; myerr = 'Couldn''t open BBSREAD''s ARexx port.'; rc = 30; signal error; end
  34.     if (rc ~= 0) then do; myerr = 'Could not find SYS:Rexxc/WaitForPort.'; rc = 30; signal error; end
  35.     end
  36.  
  37. /*
  38. ** Give template if arguments = '?' or empty
  39. */
  40.  
  41. if (arguments = '?') | (arguments = '') then do
  42.     say 'Usage: 'template
  43.     say 'SplitDigest.br is an external script for SortMail.'
  44.     exit(5)
  45.     end
  46.  
  47. address(bbsread)
  48. 'READARGS "'template'" 'args' CMDLINE 'arguments
  49. if (rc ~= 0) then do
  50.     say BBSREAD.LASTERROR
  51.     say 'Template: 'template
  52.     say 'SplitDigest.br is an external script for SortMail.'
  53.     exit(5)
  54.     end
  55.  
  56. /*
  57. ** Utilize BBSRead's copyback buffer
  58. */
  59.  
  60. address(bbsread)
  61. 'BUFMODE COPYBACK'
  62.  
  63. /*
  64. ** Read message's header and text stems
  65. */
  66.  
  67. 'READBRMESSAGE "'args.SYSTEM'" "'args.CONFERENCE'" 'args.MSGNO' HEADSTEM 'head' DATASTEM 'data' TEXTSTEM 'text
  68. if rc ~= 0 then signal error
  69.  
  70. if symbol('text.TEXT.COUNT') = 'VAR' & text.TEXT.COUNT > 0 & symbol('text.PART.COUNT') = 'VAR' & text.PART.COUNT > 0 then do
  71.     myerr = 'Message contains both a text body and text parts.'; rc = 5; signal error
  72.     end
  73.  
  74. if symbol('text.PART.COUNT') = 'VAR' & text.PART.COUNT > 0 then do
  75.     do i = 1 to text.PART.COUNT
  76.         if (symbol('text.PART.'i'.TEXT.COUNT') = 'VAR' | symbol('text.PART.'i'.COMMENT.COUNT') = 'VAR' | symbol('text.PART.'i'.BINARY') = 'VAR') then do
  77.             myerr = 'MIME digest contains a non-text part.'; rc = 5; signal error
  78.             end
  79.  
  80.         if symbol('text.PART.'i'.MSG.FROMNAME')        ~= 'VAR' then do
  81.             if symbol('text.PART.'i'.MSG.PART.1.MSG.FROMNAME') = 'VAR' then text.PART.i.MSG.fromname = text.PART.i.MSG.PART.1.MSG.FROMNAME
  82.             else text.PART.i.MSG.fromname = head.FROMNAME
  83.             end
  84.         if symbol('text.PART.'i'.MSG.FROMADDR')        ~= 'VAR' then do
  85.             if symbol('text.PART.'i'.MSG.PART.1.MSG.FROMADDR') = 'VAR' then text.PART.i.MSG.FROMADDR = text.PART.i.MSG.PART.1.MSG.FROMADDR
  86.             else text.PART.i.MSG.FROMADDR = head.FROMADDR
  87.             end
  88.         if symbol('text.PART.'i'.MSG.TONAME')          ~= 'VAR' then do
  89.             if symbol('text.PART.'i'.MSG.PART.1.MSG.TONAME') = 'VAR' then text.PART.i.MSG.TONAME = text.PART.i.MSG.PART.1.MSG.TONAME
  90.             else text.PART.i.MSG.TONAME = head.TONAME
  91.             end
  92.         if symbol('text.PART.'i'.MSG.TOADDR')          ~= 'VAR' then do
  93.             if symbol('text.PART.'i'.MSG.PART.1.MSG.TOADDR') = 'VAR' then text.PART.i.MSG.TOADDR = text.PART.i.MSG.PART.1.MSG.TOADDR
  94.             else text.PART.i.MSG.TOADDR = head.TOADDR
  95.             end
  96.         if symbol('text.PART.'i'.MSG.REFID')           ~= 'VAR' then do
  97.             if symbol('text.PART.'i'.MSG.PART.1.MSG.REFID') = 'VAR' then text.PART.i.MSG.REFID = text.PART.i.MSG.PART.1.MSG.REFID
  98.             else text.PART.i.MSG.REFID = head.REFID
  99.             end
  100.         if symbol('text.PART.'i'.MSG.CREATIONDATE')    ~= 'VAR' then do
  101.             if symbol('text.PART.'i'.MSG.PART.1.MSG.CREATIONDATE') = 'VAR' then text.PART.i.MSG.CREATIONDATE = text.PART.i.MSG.PART.1.MSG.CREATIONDATE
  102.             else text.PART.i.MSG.CREATIONDATE = head.CREATIONDATE
  103.             end
  104.         if symbol('text.PART.'i'.MSG.CREATIONDATETXT') ~= 'VAR' then do
  105.             if symbol('text.PART.'i'.MSG.PART.1.MSG.CREATIONDATETXT') = 'VAR' then text.PART.i.MSG.CREATIONDATETXT = text.PART.i.MSG.PART.1.MSG.CREATIONDATETXT
  106.             else text.PART.i.MSG.CREATIONDATETXT = head.CREATIONDATETXT
  107.             end
  108.         if symbol('text.PART.'i'.MSG.SUBJECT')         ~= 'VAR' then do
  109.             if symbol('text.PART.'i'.MSG.PART.1.MSG.SUBJECT') = 'VAR' then text.PART.i.MSG.SUBJECT = text.PART.i.MSG.PART.1.MSG.SUBJECT
  110.             else text.PART.i.MSG.SUBJECT = head.SUBJECT
  111.             end
  112.  
  113.         if symbol('args.REPLYADDR') ~= 'VAR' then do
  114.             if symbol('text.replyaddr') = 'VAR' then do
  115.                 text.PART.i.MSG.replyaddr = text.replyaddr
  116.                 if symbol('text.replyname') = 'VAR' then text.PART.i.MSG.replyname = text.replyname
  117.                 end
  118.             else do
  119.                 text.PART.i.MSG.replyaddr = head.fromaddr
  120.                 if symbol('head.fromname') = 'VAR' then text.PART.i.MSG.replyname = head.fromname
  121.                 end
  122.             end
  123.         else text.PART.i.MSG.replyaddr = args.REPLYADDR
  124.  
  125.         text.PART.i.MSG.replyconf = args.CONFERENCE
  126.         call writemessage(SYSTEM '"'args.DESTSYS'"' CONFERENCE '"'args.DESTCONF'"' MSGSTEM value(text.PART.'i'.MSG))
  127.         end
  128.     end
  129. else do
  130.     line = 1; parsed = 0
  131.  
  132.     do forever
  133.         newmsg.text.count = 0
  134.  
  135.         fromline = 0; subjline = 0; dateline = 0
  136.  
  137.         do until (text.TEXT.line = '') & (fromline ~= 0)
  138.             select
  139.                 when upper(subword(text.TEXT.line, 1, 1)) = "FROM:" then fromline = line
  140.                 when upper(subword(text.TEXT.line, 1, 1)) = "SUBJECT:" then subjline = line
  141.                 when upper(subword(text.TEXT.line, 1, 1)) = "DATE:" then dateline = line
  142.                 when line > text.TEXT.count then do
  143.                     if ~parsed then do
  144.                         myerr = 'Unexpected end of message.'; rc = 5; signal error
  145.                         end
  146.                     else return(0)
  147.                     end
  148.                 otherwise nop
  149.                 end
  150.  
  151.             line = line + 1
  152.             end
  153.  
  154.         /* Pick up date line and skip blank lines */
  155.  
  156.         do forever
  157.             if line = text.TEXT.count then do
  158.                 myerr = 'Failed to find start of first submessage''s text body.'; rc = 5; signal error
  159.                 end
  160.             if upper(subword(text.TEXT.line, 1, 1)) = "DATE:" then dateline = line
  161.             else if text.TEXT.line ~= '' then break
  162.             line = line + 1
  163.             end
  164.  
  165.         /* Search for 'End of message' line or the end of the digest */
  166.  
  167.         newmsg.text.count = 0; msgline = 0; foundend = 0
  168.         do forever
  169.             notthisone = 0
  170.  
  171.             if line = text.TEXT.count then do
  172.                 myerr = 'Immature end of message.'; rc = 5; signal error
  173.                 end
  174.  
  175.             if (symbol('endsubmsg') ~= 'VAR') & (upper(subword(text.TEXT.line, 1, 1)) = "FROM:") then do
  176.                 myerr = 'End of submessage not detected.'; rc = 5; signal error
  177.                 end
  178.  
  179.             if symbol('endsubmsg') ~= 'VAR' then do
  180.                 if (left(text.TEXT.line, 1) = '-') & (length(text.TEXT.line) > 29) & (length(text.TEXT.line) = length(compress(text.TEXT.line))) & (text.TEXT.line = copies(left(text.TEXT.line, 1), length(text.TEXT.line))) then do
  181.                     templine = line + 1
  182.                     do until (foundend) | (notthisone) | (templine = text.TEXT.count)
  183.                         if (text.TEXT.templine ~= '') then do
  184.                             if (upper(subword(text.TEXT.templine, 1, 1)) = "FROM:") | (upper(subword(text.TEXT.templine, 1, 1)) = "DATE:") | (upper(subword(text.TEXT.templine, 1, 1)) = "SUBJECT:") | (upper(left(text.TEXT.templine, 9)) = 'MESSAGE #') then do
  185.                                 endsubmsg = text.TEXT.line; foundend = 1
  186.                                 end
  187.                             else if ((templine + 7) <= text.TEXT.COUNT) then notthisone = 1; else foundend = 1
  188.                             end
  189.                         templine = templine + 1
  190.                         end
  191.                     end
  192.                 end
  193.             else if compare(endsubmsg, text.TEXT.line) = 0 then foundend = 1
  194.  
  195.             if foundend then break
  196.  
  197.             msgline = msgline + 1; newmsg.text.msgline = text.text.line
  198.             newmsg.text.count = newmsg.text.count + 1
  199.             line = line + 1
  200.             end
  201.  
  202.         /* Some magic to find name, address and subject */
  203.  
  204.         newmsg.subject = "<no subject>"
  205.         newmsg.fromname = "Unknown"
  206.         newmsg.fromaddr = "<no address>"
  207.  
  208.         from = strip(substr(text.TEXT.fromline, 6))
  209.         from = translate(from, '<>', '()')
  210.         i = pos("<", from)
  211.  
  212.         if (i ~= 0) & (pos(">", from) > 0) then do
  213.             checkaddr = strip(substr(from, i, pos('>', from) - i), B, ' <>"')
  214.             if pos("@", checkaddr) = 0 then do
  215.                 newmsg.fromname = checkaddr
  216.                 newmsg.fromaddr = strip(delstr(from, i, pos('>', from) - i), B, ' >')
  217.                 end
  218.             else do
  219.                 newmsg.fromaddr = checkaddr
  220.                 newmsg.fromname = strip(delstr(from, i, pos('>', from) - i), B, ' ">')
  221.                 end
  222.             end
  223.         else do
  224.             if pos("@", from) = 0 then do
  225.                 newmsg.fromname = strip(from, B, ' <>"')
  226.                 end
  227.             else do
  228.                 newmsg.fromaddr = strip(from, B, ' <>"')
  229.                 end
  230.             end
  231.  
  232.         if subjline ~= 0 then newmsg.subject = strip(subword(text.TEXT.subjline, 2))
  233.         if dateline ~= 0 then newmsg.creationdatetxt = strip(subword(text.TEXT.dateline, 2))
  234.  
  235.         /* Removed msgid copying to avoid multiple messages with the same message id
  236.         if symbol('head.MSGID')           ~= 'VAR' then newmsg.msgid             = head.MSGID
  237.         */
  238.  
  239.         /* Find correct reply address */
  240.  
  241.         if (args.REPLYADDR = '') | (right(args.REPLYADDR, 9) = 'REPLYADDR') then do
  242.             if symbol('TEXT.replyaddr') = 'VAR' then do
  243.                 newmsg.replyaddr = TEXT.replyaddr
  244.                 if symbol('TEXT.replyname') = 'VAR' then newmsg.replyname = TEXT.replyname
  245.                 end
  246.             else do
  247.                 newmsg.replyaddr = HEAD.fromaddr
  248.                 if symbol('HEAD.fromname') = 'VAR' then newmsg.replyname = HEAD.fromname
  249.                 end
  250.             end
  251.         else newmsg.replyaddr = args.REPLYADDR
  252.  
  253.         newmsg.replyconf = args.CONFERENCE
  254.  
  255.         call writemessage(SYSTEM '"'args.DESTSYS'"' CONFERENCE '"'args.DESTCONF'"' MSGSTEM newmsg)
  256.  
  257.         drop newmsg.; parsed = 1
  258.         end
  259.     end
  260.  
  261. returned = 0; signal cleanup
  262.  
  263. /*
  264. ** Some error detection stuff
  265. */
  266.  
  267. error:
  268. syntax:
  269.  
  270. returned = rc
  271.  
  272. select
  273.     when symbol('BBSREAD.LASTERROR') = 'VAR' then say 'Line 'sigl' returned 'returned': 'BBSREAD.LASTERROR
  274.     when symbol('myerr') = 'VAR' then say 'Line 'sigl' returned 'returned': 'myerr
  275.     otherwise say 'Line 'sigl' returned 'returned': 'errortext(returned)
  276.     end
  277.  
  278. break_c:
  279. halt:
  280. cleanup:
  281.  
  282. /*
  283. ** Turn off copyback buffer
  284. */
  285.  
  286. address(bbsread)
  287. 'BUFMODE ENDCOPYBACK'
  288.  
  289. exit(returned)
  290.  
  291.  
  292.  /****************************************************************************
  293. ************************** Write message to database **************************
  294.  ****************************************************************************/
  295.  
  296. writemessage: interpret 'procedure expose 'globals
  297.               parse arg wmarguments
  298.  
  299. /*
  300. ** Initialize arguments and parse them
  301. */
  302.  
  303. wmtemplate = 'SYSTEM/A,CONFERENCE/A,MSGSTEM/A,DONTMARKMESSAGE/S,REPLIED/S,PRIVATE/S,KEEP/S,READ/S,URGENT/S,IMPORTANT/S,CONFIDENTIAL/S,HAZELEVEL/K/N'
  304.  
  305. CDB_MARK_OWN_MSGS      = 22           /* Also mark messages from user when adding messages. */
  306. CDF_NOT_ON_BBS         = '00008000'x  /* This conference is not on the bbs. */
  307. CDNT_MAILFOLDER        = 3            /* This conference is a virtual mail folder */
  308.  
  309. wmargs.DONTMARKMESSAGE = 0; wmargs.PRIVATE      = 0; wmargs.READ = 0;    wmargs.URGENT  = 0
  310. wmargs.IMPORTANT       = 0; wmargs.CONFIDENTIAL = 0; wmargs.KEEP = 0;    wmargs.REPLIED = 0
  311. wmargs.HAZELEVEL       = 0
  312.  
  313. address(bbsread)
  314. 'READARGS 'wmtemplate wmargs' CMDLINE 'wmarguments
  315. if rc ~= 0 then signal error
  316.  
  317. /*
  318. ** See if the conference the msg will be written to exists
  319. */
  320.  
  321. 'GETCONFLIST BBSNAME "'wmargs.SYSTEM'" STEM 'conflist
  322. if rc ~= 0 then signal error
  323. conflist.system = wmargs.SYSTEM
  324.  
  325. do n = 1 to conflist.COUNT + 1 while upper(wmargs.CONFERENCE) ~= upper(conflist.n)
  326.     if n = conflist.COUNT + 1 then do
  327.         /* Create the new conference */
  328.         'CONFIGCONF "'wmargs.SYSTEM'" "'wmargs.CONFERENCE'" SET 'c2x(CDF_NOT_ON_BBS)' CONFNETTYPE 'CDNT_MAILFOLDER
  329.         if (rc ~= 0) then signal error
  330.         conflist.n = toconf
  331.         conflist.COUNT = conflist.COUNT + 1
  332.         end
  333.     end
  334.  
  335. /*
  336. ** If Show own messages isn't activated in conference mark then don't
  337. ** mark message as unread.
  338. */
  339.  
  340. 'GETCONFDATA "'wmargs.SYSTEM'" "'wmargs.CONFERENCE'" STEM 'confdata
  341. if (rc ~= 0) then signal error
  342.  
  343. if (confdata.CONFTYPE ~= CDNT_MAILFOLDER) then do
  344.     'CONFIGCONF "'wmargs.SYSTEM'" "'wmargs.CONFERENCE'" SET 'c2x(CDF_NOT_ON_BBS)' CONFNETTYPE 'CDNT_MAILFOLDER
  345.     if (rc ~= 0) then signal error        
  346.     end
  347.  
  348. /*
  349. Need data. and bbsdata.
  350. if ~bittst(confdata.FLAGS, CDB_MARK_OWN_MSGS) & (value(wmargs.msgstem'.fromaddr') = bbsdata.EMAILADDR) then wmargs.DONTMARKMESSAGE = 1
  351. */
  352.  
  353. /*
  354. ** Set the selected message flags
  355. */
  356.  
  357. writeflags = ''
  358. if wmargs.DONTMARKMESSAGE then writeflags = writeflags'DONTMARKMESSAGE '
  359. if wmargs.PRIVATE         then writeflags = writeflags'PRIVATE '
  360. if wmargs.READ            then writeflags = writeflags'READ '
  361. if wmargs.URGENT          then writeflags = writeflags'URGENT '
  362. if wmargs.IMPORTANT       then writeflags = writeflags'IMPORTANT '
  363. if wmargs.CONFIDENTIAL    then writeflags = writeflags'CONFIDENTIAL '
  364.  
  365. updateflags = ''
  366. if wmargs.KEEP            then updateflags = updateflags'SETKEEP '
  367. if wmargs.REPLIED         then updateflags = updateflags'SETREPLIED '
  368. if wmargs.HAZELEVEL > 0   then updateflags = updateflags'HAZELEVEL 'wmargs.HAZELEVEL' '
  369.  
  370. /*
  371. ** Write the message
  372. */
  373.  
  374. address(bbsread)
  375. 'WRITEBRMESSAGE "'wmargs.SYSTEM'" "'wmargs.CONFERENCE'" STEM 'wmargs.MSGSTEM
  376. if rc ~= 0 then signal error
  377. msgnr = result
  378.  
  379. /*
  380. ** Give the new message it's flags
  381. */
  382.  
  383. if updateflags ~= '' then do
  384.     'UPDATEBRMESSAGE "'wmargs.SYSTEM'" "'wmargs.CONFERENCE'" 'msgnr updateflags
  385.     if rc ~= 0 then signal error
  386.     end
  387.  
  388. return(0)
  389.